import { Component, OnInit, AfterViewInit } from '@angular/core';
import { Lodop, LodopService } from '@shared/apes/lodop';
import { ApesMessageService } from '@shared/apes/message';
import { ApiService } from '@core/services/http/api.service';


@Component({
  selector: 'apes-demo04',
  templateUrl: './demo04.component.html',
  styleUrls: ['./demo04.component.scss'],
})
export class Demo04Component  {

  /*apes-header data*/
  buttonList = [
    { label: '新增', style: 'highlight', id: 'add' },
    { label: '编辑', style: 'primary', id: 'edit' },
    { label: '修改', style: 'default', id: 'update' },
    { label: '退回', style: 'default', id: 'back' },
    { label: '提交', style: 'disabled', id: 'submit' },
  ];

  statusList = [
    { step: 'one', label: '采购申请', states: 'sent' },
    { step: 'one', label: '采购订单', states: 'purchase' },
    { step: 'one', label: '已完成', states: 'done' },
    { step: 'one', label: '已取消', states: 'cancel' },
  ];

  panelItem = [
    {
      headerName: '维度', showContent: false, prompt: 3, group: [
        { id: 1, label: '品牌', tip: '安装该APP的设备品牌', groupName: '终端', checked: false },
        { id: 2, label: '设备型号', tip: '安装该APP的设备型号', groupName: '终端', checked: false },
        { id: 3, label: '操作系统', tip: '安装该APP的操作系统', groupName: '终端', checked: false },
        { id: 4, label: '分辨率', tip: '安装该APP的分辨率', groupName: '终端', checked: false },
        { id: 5, label: '运营商', tip: '安装该APP的运营商', groupName: '终端', checked: false },
        { id: 6, label: 'App版本', tip: '安装该APP的App版本', groupName: '终端', checked: false },
        { id: 7, label: '用户类型', tip: '新用户、老用户', groupName: '用户', checked: false },
      ],
    },
    {
      headerName: '指标', showContent: false, prompt: 3, group: [
        { id: 8, label: '启动次数', tip: '安装该APP的用户', groupName: '用户', checked: false },
        { id: 9, label: '新用户数', tip: '安装该APP的用户', groupName: '用户', checked: false },
        { id: 10, label: '新用户占比', tip: '安装该APP的用户', groupName: '用户', checked: false },
        { id: 11, label: '老用户数', tip: '安装该APP的用户', groupName: '用户', checked: false },
        { id: 12, label: '老用户占比', tip: '安装该APP的用户', groupName: '用户', checked: false },
        { id: 13, label: '启动用户数', tip: '安装该APP的用户', groupName: '用户', checked: false },
        { id: 14, label: '事件触发数', tip: '安装该APP的事件', groupName: '事件', checked: false },
        { id: 15, label: '事件平均使用时长', tip: '安装该APP的事件', groupName: '事件', checked: false },
      ],
    },
  ];

  doing = false;
  error = false;
  lodop: Lodop = null;
  context: any = {
    标题: '自定义标题',
    费用: '100.00 元'
  };
//   code = `LODOP.PRINT_INITA(10,10,762,533,"测试套打");
// LODOP.ADD_PRINT_TEXT(38,78,408,30,"{{标题}}");
// LODOP.SET_PRINT_STYLEA(0,"FontSize",15);
// LODOP.SET_PRINT_STYLEA(0,"FontColor","#800000");
// LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
// LODOP.ADD_PRINT_TEXT(259,579,100,23,"{{费用}}");
// LODOP.ADD_PRINT_TEXT(260,520,58,24,"合计：");`;
  code = `LODOP.PRINT_INITA(10,10,762,533,"测试套打");
ADD_PRINT_URL(10,10,10,10,"www.baidu.com");`;

  constructor(public lodopSrv: LodopService, private msg: ApesMessageService, private apiService: ApiService) {
    this.lodopSrv.lodop.subscribe(({ lodop, ok }) => {
      if (!ok) {
        this.error = true;
        return;
      }
      this.error = false;
      this.lodop = lodop;
    });
  }

  design() {
    this.lodopSrv.attachCode(this.code);
    this.doing = true;
    this.lodopSrv.design().then((code) => {
      this.code = code;
      this.doing = false;
    });
  }

  setup() {
    this.lodopSrv.attachCode(this.code);
    this.lodop.PRINT_SETUP();
  }

  print() {
    this.lodopSrv.attachCode(this.code, this.context);
    this.lodop.PREVIEW();
  }

  printBatch() {
    this.doing = true;
    const data = new Array(3).fill({}).map((item, index) => Object.assign({ index: index + 1 }, this.context));
    this.lodopSrv.print(this.code + `
    LODOP.ADD_PRINT_TEXT(10,10,100,100,"第{{index}}张");
    `, data);
    const batch$ = this.lodopSrv.events.subscribe((res) => {
      console.log('finish', res);
      if (res.item.index === data.length - 1) {
        this.msg.success(`全部打印完成`);
        this.doing = false;
        batch$.unsubscribe();
      }
    });
  }

  method:string;
  params:string;
  sign:string;

  getSign() {
    let signParams = JSON.parse(this.params);

    this.sign = this.apiService.getSign(this.method, signParams);
    // this.sign = this.apiService.getSign(this.method, this.params);
  }

}



